<template>
  <div class="register">
    <div class="register-container">
      <div class="register-header">
        <h2>用户注册</h2>
        <p>创建您的新账号</p>
      </div>
      
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            id="username"
            v-model="registerForm.username"
            type="text"
            placeholder="请输入用户名"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            id="email"
            v-model="registerForm.email"
            type="email"
            placeholder="请输入邮箱地址"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            id="password"
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            id="confirmPassword"
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            required
          />
          <span v-if="passwordMismatch" class="error-text">密码不匹配</span>
        </div>
        
        <div class="form-group">
          <label class="checkbox-label">
            <input v-model="registerForm.agree" type="checkbox" required />
            我同意 <a href="#" @click.prevent>用户协议</a> 和 <a href="#" @click.prevent>隐私政策</a>
          </label>
        </div>
        
        <button type="submit" class="register-btn" :disabled="isLoading || passwordMismatch || !registerForm.agree">
          {{ isLoading ? '注册中...' : '注册' }}
        </button>
      </form>
      
      <div class="register-footer">
        <p>已有账号？ <router-link to="/login">立即登录</router-link></p>
        <router-link to="/" class="back-home">返回首页</router-link>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  agree: false
})

const isLoading = ref(false)

const passwordMismatch = computed(() => {
  return registerForm.password !== '' && 
         registerForm.confirmPassword !== '' && 
         registerForm.password !== registerForm.confirmPassword
})

const handleRegister = async () => {
  if (passwordMismatch.value) {
    alert('密码不匹配，请重新输入')
    return
  }
  
  isLoading.value = true
  
  // 模拟注册请求
  setTimeout(() => {
    console.log('注册信息:', registerForm)
    alert(`欢迎 ${registerForm.username}！注册成功`)
    isLoading.value = false
    router.push('/login')
  }, 1500)
}
</script>

<style scoped>
.register {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 2rem;
}

.register-container {
  background: white;
  padding: 3rem;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 450px;
}

.register-header {
  text-align: center;
  margin-bottom: 2rem;
}

.register-header h2 {
  color: #2c3e50;
  margin-bottom: 0.5rem;
}

.register-header p {
  color: #7f8c8d;
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  position: relative;
}

.form-group label {
  margin-bottom: 0.5rem;
  color: #2c3e50;
  font-weight: 600;
}

.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="password"] {
  padding: 0.75rem;
  border: 2px solid #e1e8ed;
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
}

.form-group input[type="text"]:focus,
.form-group input[type="email"]:focus,
.form-group input[type="password"]:focus {
  outline: none;
  border-color: #3498db;
}

.checkbox-label {
  display: flex !important;
  flex-direction: row !important;
  align-items: flex-start;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 0.9rem;
}

.checkbox-label input[type="checkbox"] {
  margin: 0;
  margin-top: 0.2rem;
}

.checkbox-label a {
  color: #3498db;
  text-decoration: none;
}

.checkbox-label a:hover {
  text-decoration: underline;
}

.error-text {
  color: #e74c3c;
  font-size: 0.8rem;
  margin-top: 0.25rem;
}

.register-btn {
  background: linear-gradient(135deg, #27ae60, #229954);
  color: white;
  border: none;
  padding: 0.75rem;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.register-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(39, 174, 96, 0.4);
}

.register-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.register-footer {
  text-align: center;
  margin-top: 2rem;
  padding-top: 2rem;
  border-top: 1px solid #e1e8ed;
}

.register-footer p {
  color: #7f8c8d;
  margin-bottom: 1rem;
}

.register-footer a {
  color: #3498db;
  text-decoration: none;
  font-weight: 600;
}

.register-footer a:hover {
  text-decoration: underline;
}

.back-home {
  display: inline-block;
  padding: 0.5rem 1rem;
  background-color: #95a5a6;
  color: white !important;
  border-radius: 6px;
  transition: background-color 0.3s ease;
}

.back-home:hover {
  background-color: #7f8c8d;
  text-decoration: none !important;
}
</style>